package com.xframework.exception;

//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.TypeReference;

import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ExceptionErrorDecoder implements ErrorDecoder {

    private static Logger logger = LoggerFactory.getLogger(BusinessException.class);
    private static String EXCEPTION_MESSAGE = "系统异常 请联系管理员";

    @Override
    public Exception decode(String methodKey, Response response) {
        System.out.println("decode(String methodKey, Response response)decode(String methodKey, Response response)decode(String methodKey, Response response)");
        try {
            String body = Util.toString(response.body().asReader());
//            ExceptionInfo exceptionInfo = JSON.parseObject(body, new TypeReference<ExceptionInfo>() {
//            });
            logger.error(body);
//            logger.error(exceptionInfo.getPath());
//            logger.error(exceptionInfo.getMessage());
//            logger.error(exceptionInfo.getError());
            return new BusinessException(EXCEPTION_MESSAGE);
        } catch (Exception e) {
            return new BusinessException(e);
        }
    }
}